package com.xiaoyu.lanling.feature.chat.handler

import android.content.DialogInterface
import android.view.View
import android.view.View.OnLongClickListener
import android.widget.TextView
import androidx.appcompat.app.AlertDialog
import com.xiaoyu.base.app.AppContext
import com.xiaoyu.base.utils.extensions.getViewTag
import com.xiaoyu.base.utils.extensions.setViewTag
import com.xiaoyu.lanling.R
import com.xiaoyu.lanling.feature.chat.model.message.ChatMessageReceiveAudioItem
import com.xiaoyu.lanling.feature.chat.model.message.ChatMessageSendAudioItem
import com.xiaoyu.lanling.feature.chat.util.AudioViewHolderAnimUtils
import com.xiaoyu.lanling.feature.im.provider.MessageDataProvider

object ChatMessageAudioItemClickHandler {

    private val receiveAudioOnLongClickListener = OnLongClickListener { v: View ->
        val itemData: ChatMessageReceiveAudioItem = v.getViewTag<ChatMessageReceiveAudioItem>()
                ?: return@OnLongClickListener false
        val items = arrayOf<CharSequence>(AppContext.getString(R.string.action_delete))
        AlertDialog.Builder(v.context)
                .setItems(items) { dialog: DialogInterface, which: Int ->
                    when (which) {
                        0 -> MessageDataProvider.instance.deleteMessageAsync(itemData.message)
                    }
                    dialog.dismiss()
                }.show()
        true
    }
    private val sendAudioOnLongClickListener = OnLongClickListener { v: View ->
        val itemData: ChatMessageSendAudioItem = v.getViewTag<ChatMessageSendAudioItem>()
                ?: return@OnLongClickListener false
        val items = arrayOf<CharSequence>(AppContext.getString(R.string.action_delete))
        AlertDialog.Builder(v.context)
                .setItems(items) { dialog: DialogInterface, which: Int ->
                    when (which) {
                        0 -> MessageDataProvider.instance.deleteMessageAsync(itemData.message)
                    }
                    dialog.dismiss()
                }.show()
        true
    }
    private val receiveAudioOnClickListener = View.OnClickListener { v: View ->
        val itemData: ChatMessageReceiveAudioItem = v.getViewTag<ChatMessageReceiveAudioItem>()
                ?: return@OnClickListener
        val textView = v as TextView?
        AudioViewHolderAnimUtils.getInstance().play(itemData.message, itemData.audioFilePath, textView, true)
        itemData.mAutoPlay = false
    }
    private val sendAudioOnClickListener = View.OnClickListener { v: View ->
        val itemData: ChatMessageSendAudioItem = v.getViewTag<ChatMessageSendAudioItem>()
                ?: return@OnClickListener
        val textView = v as TextView?
        AudioViewHolderAnimUtils.getInstance().play(itemData.message, itemData.audioFilePath, textView, false)
    }

    fun bindReceiveAudioItemLongClickForViewAndData(view: View, itemData: ChatMessageReceiveAudioItem?) {
        view.setOnLongClickListener(receiveAudioOnLongClickListener)
        view.setViewTag(itemData)
    }

    fun bindSendAudioItemLongClickForViewAndData(view: View, itemData: ChatMessageSendAudioItem?) {
        view.setOnLongClickListener(sendAudioOnLongClickListener)
        view.setViewTag(itemData)
    }

    fun bindReceiveAudioItemClickForViewAndData(view: View, itemData: ChatMessageReceiveAudioItem?) {
        view.setOnClickListener(receiveAudioOnClickListener)
        view.setViewTag(itemData)
    }

    fun bindSendAudioItemClickForViewAndData(view: View, itemData: ChatMessageSendAudioItem?) {
        view.setOnClickListener(sendAudioOnClickListener)
        view.setViewTag(itemData)
    }
}